﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Render;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Editor;
using System.Drawing.Imaging;
using ITAP.Core.Calculations;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Operations {
    public class OperationGetModelsWmf : IOperation
    {
        private List<Metafile> metafiles;

        public List<Metafile> Metafiles {
            get {
                return metafiles;
            }
        }
        #region IOperation Members

        public string Name {
            get {
                return "Получить Wmf моделей";
            }
        }

        public OperationGroup OperationGroup {
            get {
                return OperationGroup.Other;
            }
        }

        public System.Drawing.Image Thumbnail {
            get {
                return null;
            }
        }

        public void Execute(ModelControl modelControl) {
            ModelCoordinateConverter coordinateConverter = new ModelCoordinateConverter();
            DraftRender render = new DraftRender();
            metafiles = new List<Metafile>();


            foreach(ModelCell modelCell in modelControl.ModelContainer.InnerComponents) {

                render.SetDimensionsPreferences(GlobalPreferences.Preferences.DraftDimensions);

                RectangleF modelRect = modelCell.Model.BoundingBox;
                ModelPoint center = new ModelPoint(modelRect.X + modelRect.Width / 2, modelRect.Y + modelRect.Height / 2);
                RectangleF screenRect = coordinateConverter.ModelRectangleToScreenRectangle(modelRect, (int)modelRect.Width, (int)modelRect.Height, modelRect, center);
                Bitmap bitmap = new Bitmap((int)screenRect.Width, (int)screenRect.Height);
                Graphics ig = Graphics.FromImage(bitmap);

                render.Initialize(ig, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                render.SetSizesFont(GlobalPreferences.Preferences.DraftDimensions.Font);

                modelRect = render.GetModelBoundingBoxAfterDrawing(modelCell, (int)screenRect.Width, (int)screenRect.Height);
                center = new ModelPoint(modelRect.X + modelRect.Width / 2, modelRect.Y + modelRect.Height / 2);
                screenRect = coordinateConverter.ModelRectangleToScreenRectangle(modelRect, (int)modelRect.Width, (int)modelRect.Height, modelRect, center);
                render.Initialize(ig, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                render.SetSizesFont(GlobalPreferences.Preferences.DraftDimensions.Font);
                render.Visit(modelCell);
                IntPtr hdc = ig.GetHdc();
                Metafile metafile = new Metafile(hdc, screenRect, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
                ig.ReleaseHdc();
                Graphics mg = Graphics.FromImage(metafile);
                mg.PageUnit = GraphicsUnit.Pixel;
                mg.Clear(Color.White);
                render.Initialize(mg, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                render.SetSizesFont(GlobalPreferences.Preferences.DraftDimensions.Font);
                render.Visit(modelControl.ModelContainer);
                metafiles.Add(metafile);
                mg.Dispose();
                ig.Dispose();
            }
        }

        public bool CanUndo {
            get {
                return false;
            }
        }

        #endregion
    }
}
